/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */

package io.renren.modules.project.controller;

import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.SftpATTRS;
import com.jcraft.jsch.SftpException;
import io.renren.common.annotation.SysLog;
import io.renren.common.exception.RRException;
import io.renren.common.utils.DateUtils;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.common.validator.group.UpdateGroup;
import io.renren.modules.project.entity.PublishEntity;
import io.renren.modules.project.service.PublishService;
import io.renren.modules.sys.controller.AbstractController;
import io.renren.modules.systemconfig.dao.SysConfigServerDao;
import io.renren.modules.systemconfig.entity.SystemConfigServersEntity;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;
import java.util.Vector;

/**
 * 系统用户
 *
 * @author Mark sunlightcs@gmail.com
 */
@RestController
@RequestMapping("/publish")
public class PublishController extends AbstractController {
	@Autowired
	private PublishService publishService;
	@Autowired
	private SysConfigServerDao serverMapper;

	/**
	 * 所有用户列表
	 */
	@GetMapping("/list")
	@RequiresPermissions("publish:list")
	public R list(@RequestParam Map<String, Object> params){
		PageUtils page = publishService.queryPage(params);

		return R.ok().put("page", page);
	}


	/**
	 * 获取登录的用户信息
	 */
	@GetMapping("/info/{id}")
	public R info(@PathVariable("id") Long id){
		return R.ok().put("server", publishService.getById(id));
	}
	
	/**
	 * 保存用户
	 */
	@SysLog("保存用户")
	@PostMapping("/save")
	@RequiresPermissions("publish:save")
	public R save(@RequestBody PublishEntity entity){
		ValidatorUtils.validateEntity(entity, AddGroup.class);
		entity.setCreateTime(DateUtils.formatDefault(new Date()));
		publishService.saveEntity(entity);
		
		return R.ok();
	}
	
	/**
	 * 修改用户
	 */
	@SysLog("修改用户")
	@PostMapping("/update")
	@RequiresPermissions("publish:update")
	public R update(@RequestBody PublishEntity user){
		ValidatorUtils.validateEntity(user, UpdateGroup.class);
		publishService.update(user);
		
		return R.ok();
	}
	
	/**
	 * 删除用户
	 */
	@SysLog("删除用户")
	@PostMapping("/delete")
	@RequiresPermissions("publish:delete")
	public R delete(@RequestBody Long[] delIds){
		publishService.deleteBatch(delIds);
		return R.ok();
	}
	@SysLog("发布版本")
	@PostMapping("/publishVersionToRuntime")
//	@RequiresPermissions("publish:runtime")
	public R publishVersionToRuntime(@RequestParam("id") Long id){
		publishService.publishVersionToRuntime(id);
		return R.ok();
	}
	@SysLog("前端重启")
	@PostMapping("/restartFront")
//	@RequiresPermissions("publish:runtime")
	public R restartFront(@RequestParam("id") Long id){
		publishService.restartFront(id);
		return R.ok();
	}

	@SysLog("后端重启")
	@PostMapping("/restartBackend")
//	@RequiresPermissions("publish:runtime")
	public R restartBackend(@RequestParam("id") Long id){
		publishService.restartBackend(id);
		return R.ok();
	}


	@SysLog("后端重启")
	@PostMapping("/restartSources")
//	@RequiresPermissions("publish:runtime")
	public R restartSources(@RequestParam("id") Long id){
		publishService.restartSources(id);
		return R.ok();
	}


	@SysLog("解压文件")
	@PostMapping("/unzipFile")
//	@RequiresPermissions("publish:runtime")
	public R unzipFile(@RequestParam("id") Long id,@RequestParam("path") String path){
		try{
			publishService.unzipFile(id,path);
			return R.ok();
		}catch (Exception e){
			e.printStackTrace();
			return R.error("操作失败");
		}

	}
	@SysLog("删除远程文件")
	@PostMapping("/delSFTPFile")
//	@RequiresPermissions("publish:runtime")
	public R delSFTPFile(@RequestParam("id") Long id,@RequestParam("path") String path){
		try{
			SystemConfigServersEntity scs = serverMapper.selectById(id);
			publishService.delSFTPFiles(scs,path);
			return R.ok();
		}catch (Exception e){
			e.printStackTrace();
			return R.error("操作失败");
		}

	}

}
